// JavaScript Document

function jump(start) {
	if(start >= 0) {
		var curUrl = new String(window.location);
		var i = curUrl.indexOf("?");
		var u = curUrl.substr(0,i);
		var p = curUrl.substr(i+1);
		var newUrl = u+"?start="+start;
		for(var i in p.split("&")) {
			var param = p.split("&")[i];
			if(param.indexOf("filterFrom")> -1
				|| param.indexOf("filterTo")> -1
				|| param.indexOf("labels")> -1) {
				newUrl += "&";
				newUrl += p.split("&")[i];
			}
		}
		//alert(newUrl);
		window.open(newUrl,"_self");
		
	}
}

function showBillDialogAdd() {
	resetBillDialog();
	$("#bill-dialog > h2").text("Adding...");
	$("#mask").css("display","block");
	$("#billAddOptionBar").css("display","block");
	$("#billEditOptionBar").css("display","none");
	$("#bill-dialog").css("display","block");
	
	//use for testing
	$("#_bill_date").val("2008-08-08");
	amount = $("#_bill_amount").val("56");
	labels = $("#_bill_label").val("吃饭");
	remark = $("#_bill_remark").val("吃了酸菜鱼，好好吃"); 
}
function showBillDialogEdit(billId) {
	resetBillDialog();
	$("#bill-dialog > h2").text("Editing...");
	var tr = $("tr[keyString='"+billId+"'] > td");
	$("#_bill_key_string").val(billId);
	$("#_bill_date").val(tr.get(1).firstChild.innerHTML.replace(/\s/g,""));
	$("#_bill_amount").val(tr.get(2).innerHTML.replace(/\s/g,""));
	$("#_bill_label").val(tr.get(3).innerHTML.replace(/\s/g,""));
	$("#_bill_remark").val(tr.get(4).innerHTML.replace(/\s/g,"")); 
	$("#mask").css("display","block");
	$("#billAddOptionBar").css("display","none");
	$("#billEditOptionBar").css("display","block");
	$("#bill-dialog").css("display","block");

}
function shutBillDialog() {
	$("#mask").css("display","none");
	$("#bill-dialog").css("display","none");
	
}

function addBill() {
	var date = $("#_bill_date").val();
	var amount = $("#_bill_amount").val();
	var labels = $("#_bill_label").val();
	var remark = $("#_bill_remark").val(); 
	if(date ==null || date == "") {
		alert("date cannot be null");
		return;
	}
	if(amount ==null || amount == "") {
		alert("amount cannot be null");
		return;
	}
	$.ajax({
		type : "post",
		url  : "index.htm",
		data : "method=add&date="+date+"&amount="+amount+"&labels="+labels+"&remark="+remark,
		success : function(msg) {
			var id = msg.split(":")[0];
			if(id == "-1") {
			}else {
				var newBill = '<tr keyString="'+id+'" class="new">'
					+'<td><input type="checkbox" keyString="'+id+'"/></td>'
					+'<td><a href="javascript:showBillDialogEdit(\''+id+'\');">'+date+'</a></td>'
					+'<td>'+amount+'</td>'
					+'<td>'+labels+'</td>'
					+'<td>'+remark+'</td>'
				+'</tr>';
				$("#ct-table-optionbar").after(newBill);
				shutBillDialog();
				//window.location.reload();
				window.open("index.htm","_self");
			}
		}
	});
}
function addBillAndNext() {
	var date = $("#_bill_date").val();
	var amount = $("#_bill_amount").val();
	var labels = $("#_bill_label").val();
	var remark = $("#_bill_remark").val(); 
	if(date ==null || date == "") {
		alert("date cannot be null");
		return;
	}
	if(amount ==null || amount == "") {
		alert("amount cannot be null");
		return;
	}
	$.ajax({
		type : "post",
		url  : "index.htm",
		data : "method=add&date="+date+"&amount="+amount+"&labels="+labels+"&remark="+remark,
		success : function(msg) {
			var id = msg.split(":")[0];
			if(id == "-1") {
			}else {
				var newBill = '<tr keyString="'+id+'" class="new">'
					+'<td><input type="checkbox" keyString="'+id+'"/></td>'
					+'<td><a href="javascript:showBillDialogEdit(\''+id+'\');">'+date+'</a></td>'
					+'<td>'+amount+'</td>'
					+'<td>'+labels+'</td>'
					+'<td>'+remark+'</td>'
				+'</tr>';
				$("#ct-table-optionbar").after(newBill);
			}
		}
	});
}

function delBill() {
	var delIdArray = new Array();
	$("input[type='checkbox']").each(function(i) {
		if(this.checked) {
			delIdArray.push($(this).attr('keyString'));
		}
	});
	var delIdString = delIdArray.toString();
	if(delIdString == null ||delIdString == "") return;
	else {
		$.ajax({
			type:"POST",
			url:"index.htm",
			data:"method=del&delIdString="+delIdString,
			success:function(msg){
				for(var idArray in delIdArray) {
					$("tr[keyString='"+delIdArray[idArray]+"']").remove();
				}
			}
		});
	}
}

function editBill() {
	var keyString = $("#_bill_key_string").val();
	var date = $("#_bill_date").val();
	var amount = $("#_bill_amount").val();
	var labels = $("#_bill_label").val();
	var remark = $("#_bill_remark").val(); 
	var tr = $("tr[keyString='"+keyString+"'] > td");
	if(keyString == null || keyString == "") {
		alert("Key String cannot be null");
		return;
	}
	if(date ==null || date == "") {
		alert("date cannot be null");
		return;
	}
	if(amount ==null || amount == "") {
		alert("amount cannot be null");
		return;
	}
	if(labels ==null || labels == "") {
		alert("labels cannot be null");
		return;
	}
	if(remark ==null || remark == "") {
		alert("remark cannot be null");
		return;
	}
	if(tr.get(1).firstChild.innerHTML.replace(/\s/g,"") == date &&
				tr.get(2).innerHTML.replace(/\s/g,"") == amount &&
				tr.get(3).innerHTML.replace(/\s/g,"") == labels &&
				tr.get(4).innerHTML.replace(/\s/g,"") == remark) {
		return;
	}
	$.ajax({
		type : "post",
		url  : "index.htm",
		data : "method=edit&keystring="+keyString+"&date="+date+"&amount="+amount+"&labels="+labels+"&remark="+remark,
		success : function(msg) {
			var id = msg.split(":")[0];
			if(id == "-1") {
				alert(msg.split(":")[2]);
			}else {
				tr.get(1).firstChild.innerHTML = date;
				tr.get(2).innerHTML = amount;
				tr.get(3).innerHTML = labels;
				tr.get(4).innerHTML = remark; 
				shutBillDialog();
				$("tr[keyString='"+keyString+"']").addClass("new");
			}
		}
	});
	
}

function selectAll() {
	$("input[type='checkbox']").attr("checked", true);
}
function unSelectAll() {
	$("input[type='checkbox']").attr("checked", false);
}




function resetBillDialog() {
	$("#_bill_date").val("");
	$("#_bill_amount").val("");
	$("#_bill_label").val("");
	$("#_bill_remark").val("");
	$("#bill-dialog > .tip").html("");
}

//Filter
$(function() {
	Date.format = 'yyyy-mm-dd';
	
	
	$('#_bill_date').datePicker({
		clickInput:true,
		startDate:'2007-01-01'
	}).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{
				$('#_bill_date').dpSetSelected($('#_bill_date').val());
				$(this).dpDisplay();
				return false;
			}
		);
});
function showFilterByDateDialog() {
	$("#mask").css("display","block");
	$("#filter-by-date-dialog").css("display","block");
}
function shutFilterByDateDialog() {
	$("#mask").css("display","none");
	$("#filter-by-date-dialog").css("display","none");
}

function filterByDate() {
	var from = $("#filterByDateFrom").val();
	var to = $("#filterByDateTo").val();
	
}
function filterByLabel() {
	var labelArr = new Array();
	$("#label-panel > table > tbody > tr > td > a").each(function(){
		if(this.selected == 'true')
			labelArr.push($(this).html());
	});
	var labels = labelArr.join(",");
	alert(labels);
	/*
	$.ajax({
		type:"post",
		url:"index.htm",
		data:"method=getByPage",
	});
	*/
}
function showLabelPanel(obj) {
	resetLabelPanel();
	$("#mask").css("display","block");
	$("#label-panel").css("display","block");
	$("#label-panel").width($("#label-panel > table").width() +2);
	if(obj) {//select label
		var originalLabel = obj.value;
		for(var l in originalLabel.split(",")) {
			$("#label-panel > table > tbody > tr > td > a").each(function(){
				
				if(this.innerHTML == originalLabel.split(",")[l]) {
					$(this).addClass("selected");					
					this.selected="true";
				}
			});
		}
		$("#_select_label_for_bill").css("display","block");
		$("#_filter_bill_by_label").css("display","none");
		$("#label-panel").css("top",$(obj).offset().top + $(obj).height()+4);
		$("#label-panel").css("left",$(obj).offset().left);
	}else {//filter by label
		$("#_select_label_for_bill").css("display","none");
		$("#_filter_bill_by_label").css("display","block");
	}
}
function shutLabelPanel(func) {
	
	$("#label-panel").css("display","none");
	if(func == "filter") 
		$("#mask").css("display","none");
}
function resetLabelPanel() {
	$("#label-panel > table > tbody > tr > td > a").each(function(){
		$(this).removeClass("selected");
		this.selected = "false";
	});
}

function clickLabel(obj) {
	$(obj).toggleClass("selected");
	$(obj).attr("selected",(obj.selected == "true") ? "false" : "true");
}

function chooseLabel() {
	var labelArr = new Array();
	$("#label-panel > table > tbody > tr > td > a").each(function(){
		if(this.selected == 'true')
			labelArr.push($(this).html());
	});
	$("#_bill_label").val(labelArr.join(","));
	shutLabelPanel();
}
